﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace QQInterfaceDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private PerkyQQ perkyQq = null;
        private void button1_Click(object sender, EventArgs e)
        {
            perkyQq = new PerkyQQ(txt_qqNumber.Text.Trim(), txt_pwd.Text.Trim());
            if (perkyQq.QQ_Login())
            {
                foreach (string item in perkyQq.QQ_List())
                {
                    treeView1.Nodes.Add(item.Trim());
                }
            }
            Thread thread = new Thread(delegate()
                {
                    for (int i = 2; i < treeView1.Nodes.Count - 1; i++)
                    {
                        treeView1.Nodes[i].Text = treeView1.Nodes[i].Text + "|" + perkyQq.QQ_GetInfo(treeView1.Nodes[i].Text.Trim())[14];
                    }
                });
            thread.Start();

        }

        private void btn_Add_qq_Click(object sender, EventArgs e)
        {
            if (perkyQq.AddToList(txt_qqNum_Op.Text.Trim()) == "0")
            {
                MessageBox.Show("已是好友");
            }
            else if (perkyQq.AddToList(txt_qqNum_Op.Text.Trim()) == "1")
            {
                MessageBox.Show("需要身份认证请等待");
                AddQqInfoValid addQqInfoValid = new AddQqInfoValid(perkyQq, txt_qqNum_Op.Text.Trim());
                addQqInfoValid.Show();
            }
            else if (perkyQq.AddToList(txt_qqNum_Op.Text.Trim()) == "2")
            {
                MessageBox.Show("对方不请允许添加好友");
            }
            else
            {
                MessageBox.Show("未知情况");
            }
        }

        private void btn_select_qqInfo_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string msg = rb_sendMsg.Text;
            bool isSend = perkyQq.QQ_SendMsg(txt_qqNum_Op.Text, msg);
            if (isSend)
            {
                rb_msg.Text += msg + "\r\n";
            }
            else
            {
                rb_msg.Text += msg + "\r\n" + "发送失败";
            }
            rb_sendMsg.Text = "";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (perkyQq!=null)
            {
                int time = DateTime.Now.Second;
                if (time % 2 == 0)
                {
                    perkyQq.GetMsgEx();
                    rb_msg.Text += perkyQq.MG;
                }
            }
        }
    }
}
